package com.workoutgrabber.ui.panels;

import static java.awt.Color.BLACK;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.joda.time.DateTime;
import org.netbeans.validation.api.builtin.Validators;
import org.netbeans.validation.api.ui.ValidationGroup;
import org.netbeans.validation.api.ui.ValidationPanel;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.toedter.calendar.JDateChooser;
import com.workoutgrabber.model.ExerciseSet;
import com.workoutgrabber.model.WeightExercise;
import com.workoutgrabber.ui.listeners.ExerciseCreatedEvent;
import com.workoutgrabber.ui.listeners.ExerciseCreatedListener;
import com.workoutgrabber.ui.model.SetTableModel;

public class WeightExercisePanel extends ExercisePanel implements ActionListener {
	private JButton buttonAddWeightExercise, buttonAddSet;
	private JDateChooser weigtExerciseDate;
	private JTextField textFieldWeightExerciseName, textFieldWeight, textFieldReps; 
	private JScrollPane scrollableTable;
	private SetTableModel setTableModel;
	private ValidationPanel validateNameAndDatePanel, validateSetDetailsPanel ;
	private ValidationGroup validateNameAndDate, validateSetDetails;
	
	public WeightExercisePanel(){
		super();
		
		initComponents();
		initValidation();
		buildPanel();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == buttonAddSet){
			if(validateSetDetails.validateAll() == null){
				Double weight = Double.valueOf(textFieldWeight.getText());
				Integer reps = Integer.valueOf(textFieldReps.getText());
				setTableModel.add(ExerciseSet.getInstance(weight, reps));
			}
		}
		
		if(e.getSource() == buttonAddWeightExercise)
			fireExerciseCreatedEvent();
	}
	
//-----------------
// private methods
//-----------------
	
	private void initComponents() {
		textFieldWeightExerciseName = new JTextField();
		textFieldWeightExerciseName.setName("Name");
		
		textFieldWeight	= new JTextField();
		textFieldWeight.setName("Weight");
		
		textFieldReps = new JTextField();
		textFieldReps.setName("Reps");
		
		weigtExerciseDate = new JDateChooser();

		setTableModel = new SetTableModel();

		JTable setTable = new JTable(setTableModel);
		setTable.setFillsViewportHeight(true);

		scrollableTable	= new JScrollPane(setTable);
		scrollableTable.setPreferredSize(new Dimension(0, 100));
		scrollableTable.setBorder(BorderFactory.createLineBorder(BLACK));

		buttonAddSet = new JButton("Add set");
		buttonAddSet.addActionListener(this);
		
		buttonAddWeightExercise = new JButton("Add exercise");
		buttonAddWeightExercise.addActionListener(this);
	}
	
	private void initValidation() {
		validateNameAndDatePanel = new ValidationPanel();
		validateNameAndDate = validateNameAndDatePanel.getValidationGroup();

		validateSetDetailsPanel = new ValidationPanel();
		validateSetDetails = validateSetDetailsPanel.getValidationGroup();
		
		validateNameAndDate.add(textFieldWeightExerciseName, Validators.REQUIRE_NON_EMPTY_STRING);
		
		validateSetDetails.add(textFieldReps, Validators.REQUIRE_VALID_INTEGER, Validators.REQUIRE_NON_NEGATIVE_NUMBER);
		validateSetDetails.add(textFieldWeight, Validators.REQUIRE_VALID_NUMBER, Validators.REQUIRE_NON_NEGATIVE_NUMBER);
	}
	
	private void buildPanel(){
		String rowDefinition = "p, 5dlu, p, 3dlu, p, 3dlu, p, 5dlu, p, 5dlu, p";

		PanelBuilder builder = new PanelBuilder(new FormLayout(WorkoutPanel.COLUMN_DEFINITION, rowDefinition.toString()));
		CellConstraints cc = new CellConstraints();

		builder.addSeparator("Weight exercise", cc.xyw(1, 1, 7));
		
		builder.addLabel("Name", cc.xy (1, 3));
		builder.add(textFieldWeightExerciseName, cc.xy (3, 3));
		builder.addLabel("Date", cc.xy (5, 3));
		builder.add(weigtExerciseDate, cc.xy (7, 3));
		
		builder.addLabel("Reps", cc.xy (1, 5));
		builder.add(textFieldReps, cc.xy (3, 5));
		builder.addLabel("Weight", cc.xy (5, 5));
		builder.add(textFieldWeight, cc.xy (7, 5));
	
		builder.add(validateSetDetailsPanel, cc.xyw(1, 7, 6));
		builder.add(buttonAddSet, cc.xy(7, 7));
		
		builder.add(scrollableTable, cc.xyw(1, 9, 7));

		builder.add(validateNameAndDatePanel, cc.xyw(1, 11, 6));		
		builder.add(buttonAddWeightExercise, cc.xy(7, 11));
		
		add(builder.getPanel());
	}
	
	private void fireExerciseCreatedEvent(){
		if( validateNameAndDate.validateAll() == null ){ // TODO weird way to check for validation errors
			String name = textFieldWeightExerciseName.getText();
			DateTime date = new DateTime(weigtExerciseDate.getDate());

			ExerciseCreatedEvent event = new ExerciseCreatedEvent(this, 0, WeightExercise.getInstance(name, date, setTableModel.getSets()));

			for (ExerciseCreatedListener listener : listeners)
				listener.exerciseCreated(event);
		}
	}	
}
